package org.zero.onlinesubject.utils;

import cn.hutool.captcha.*;
import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
public class MyCaptchaUtil {
    private static final ScheduledExecutorService scheduled = Executors.newSingleThreadScheduledExecutor();
    public static final HashMap<String,Long> codeMap = new HashMap<>();

    static {
        checkExpireCode();
    }

    public void test() throws IOException {
        ShearCaptcha captcha1 = CaptchaUtil.createShearCaptcha(150, 50, 6, 5);
        System.out.println(captcha1.getCode());
        byte[] imageBytes1 = captcha1.getImageBytes();
        FileUtil.writeBytes(imageBytes1,new File("D:\\Code\\online-subject\\src\\main\\resources\\static\\imgs\\ShearCaptcha.jpg"));

        LineCaptcha captcha2 = CaptchaUtil.createLineCaptcha(150, 50, 6, 5);
        System.out.println(captcha2.getCode());
        byte[] imageBytes2 = captcha2.getImageBytes();
        FileUtil.writeBytes(imageBytes2,new File("D:\\Code\\online-subject\\src\\main\\resources\\static\\imgs\\LineCaptcha.jpg"));

        CircleCaptcha captcha3 = CaptchaUtil.createCircleCaptcha(150, 50, 6, 5);
        System.out.println(captcha3.getCode());
        byte[] imageBytes3 = captcha3.getImageBytes();
        FileUtil.writeBytes(imageBytes3,new File("D:\\Code\\online-subject\\src\\main\\resources\\static\\imgs\\CircleCaptcha.jpg"));

        GifCaptcha captcha4 = CaptchaUtil.createGifCaptcha(150, 50, 6);
        System.out.println(captcha4.getCode());
        byte[] imageBytes4 = captcha4.getImageBytes();
        FileUtil.writeBytes(imageBytes4,new File("D:\\Code\\online-subject\\src\\main\\resources\\static\\imgs\\GifCaptcha.jpg"));
    }

    public static byte[] generateCaptcha(){
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(150, 50, 6, 10);
        String code = captcha.getCode();
        Long expireTime = System.currentTimeMillis() + 10 * 60 * 1000;
        codeMap.put(code,expireTime);
        return captcha.getImageBytes();
    }

    public static boolean checkCode(String code){
        if (codeMap.containsKey(code)) {
            codeMap.remove(code);
            return true;
        }else{
            return false;
        }
    }

    private static void checkExpireCode() {
        scheduled.scheduleAtFixedRate(() -> {
            long now = System.currentTimeMillis();
            log.info("定时任务开始执行。。。");
            Iterator<Map.Entry<String, Long>> iterator = codeMap.entrySet().iterator();
            while(iterator.hasNext()){
                Map.Entry<String, Long> entry = iterator.next();
                if (entry.getValue() < now) {
                    log.info("删除过期code: {}",entry.getKey());
                    iterator.remove();
                }
            }
        }, 0, 60, TimeUnit.SECONDS);
    }

    public static String generateCode(){
        int len = 6;
        char[] nums = {'0','1','2','3','4','5','6','7','8','9'};
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        int i = 0;
        while(i<len){
            code.append(nums[random.nextInt(nums.length)]);
            i++;
        }
        return code.toString();
    }

}



